<?php
/* @var $this yii\web\View */
/* @var $model mdm\admin\models\AuthItem */
/* @var $form yii\widgets\ActiveForm */
/* @var $context mdm\admin\components\ItemController */

use yii\helpers\Json;
use yii\helpers\Html;
use common\widgets\ActiveForm;
use mdm\admin\components\RouteRule;
use mdm\admin\AutocompleteAsset;
use mdm\admin\components\Configs;

$context = $this->context;
$labels = $context->labels();
$rules = Configs::authManager()->getRules();
unset($rules[RouteRule::RULE_NAME]);
$source = Json::htmlEncode(array_keys($rules));
$js = <<<JS
    $('#rule_name').autocomplete({
        source: $source,
    });
JS;
AutocompleteAsset::register($this);
$this->registerJs($js);
?>
<div class="auth-item-form">
    <?php
    $form = ActiveForm::begin([
        'id' => 'item-form',
        'layout' => 'horizontal',
        'fieldConfig' => [
            'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
            'horizontalCssClasses' => [
                'label' => 'col-sm-2',
                'offset' => 'col-sm-offset-2',
                'wrapper' => 'col-sm-6',
                'error' => '',
                'hint' => '',
            ],
        ],
    ]);
    ?>
    <div class="form-body">
        <?= $form->field($model, 'name')->textInput(['maxlength' => 64]) ?>
        <?= $form->field($model, 'description')->textarea(['rows' => 2]) ?>
        <?php
        if ($model->type == \yii\rbac\Item::TYPE_ROLE) {
            echo $form->field($model, 'ruleName')->dropDownList(null, [
                'id' => 'rule_name',
                'prompt' => [
                    'text' => '请选择...',
                    'options' => [
                        'value' => ''
                    ]
                ],
            ])->hint('* 只有选择设置了授权规则，授权的业务内容才会生效', ['class' => 'font-red']);
            echo $form->field($model, 'store')->checkboxList();
        } else {
            echo $form->field($model, 'ruleName')->textInput(['id' => 'rule_name']);
            echo $form->field($model, 'data')->textarea(['rows' => 6]);
        }
        ?>
    </div>
    <div class="form-actions">
        <div class="row">
            <div class="col-md-offset-2 col-md-10">
                <?=
                Html::submitButton($model->isNewRecord ? Yii::t('rbac-admin', 'Create') : Yii::t('rbac-admin', 'Update'), [
                    'class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-success',
                    'name' => 'submit-button'
                ])
                ?>
            </div>
        </div>
    </div>
    <?php ActiveForm::end(); ?>
</div>
